﻿using System;
using Csla;

namespace BizControl.Library.BusinessObjects.Statistics
{
  [Serializable()]
  public class TestCommand : CommandBase<TestCommand>
  {
    public static bool Execute()
    {
      TestCommand cmd = new TestCommand();
      cmd.BeforeServer();
      cmd = DataPortal.Execute<TestCommand>(cmd);
      cmd.AfterServer();
      return cmd.Result;
    }

    public static void BeginExecute(EventHandler<DataPortalResult<TestCommand>> callback)
    {
      TestCommand cmd = new TestCommand();
      cmd.BeforeServer();
      DataPortal.BeginExecute<TestCommand>(cmd, (o, e) =>
        {
          if (e.Error != null) throw e.Error;
          e.Object.AfterServer();
          callback(o, e);
        });
    }

    private static readonly PropertyInfo<bool> ResultProperty = RegisterProperty<bool>(o => o.Result);
    public bool Result
    {
      get { return ReadProperty<bool>(ResultProperty); }
      private set { LoadProperty<bool>(ResultProperty, value); }
    }

    private void BeforeServer()
    {
      // client code before server call
    }

    private void AfterServer()
    {
      // client code after server call
    }

    protected override void DataPortal_Execute()
    {
      Result = true;
    }
  }
}
